package com.hsav.kcxb;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hsav.kcxb.fragment.FindsFragment;
import com.hsav.kcxb.fragment.MessageFragment;
import com.hsav.kcxb.fragment.SingFragment;
import com.hsav.kcxb.fragment.UserFragment;
import com.hsav.kcxb.fragment.SongListFragment;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;

import static com.hsav.kcxb.main.MSYS.mMyTools;

public class MainActivity extends AutoLayoutActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private ViewPager mViewPager;
    private LinearLayout mUserTabLayout;
    private ImageView mUserIv;
    private ArrayList<Fragment> mFragmentList;
    private UserFragment mUserFragment;
    private SongListFragment mSongListFragment;
    private FragmentPagerAdapter mFragmentAdapter;
    private SingFragment mSingFragment;
    private MessageFragment mMsgFragment;
    private FindsFragment mFindsFragment;
    private ImageView mTabMyIv;
    private ImageView mTabListIv;
    private ImageView mTabSingIv;
    private ImageView mTabMsgIv;
    private ImageView mTabFindIv;
    private ArrayList<ImageView> mImageViewList;
    private TextView mTabMyTv;
    private TextView mTabListTv;
    private TextView mTabSingTv;
    private TextView mTabMsgTv;
    private TextView mTabFindTv;
    private final int COLOR_FOCOUS = 0xffffcc00;
    private final int COLOR_NORMAL = 0xff000000;
    private int mLastTab;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(R.layout.activity_main);
        mMyTools.startupExtrApp();
        initView();
        initData();
    }

    private void initData() {
        mFragmentList = new ArrayList<Fragment>();
        mUserFragment = new UserFragment();
        mSongListFragment = new SongListFragment();
        mSingFragment = new SingFragment();
        mMsgFragment = new MessageFragment();
        mFindsFragment = new FindsFragment();

        mFragmentList.add(mUserFragment);
        mFragmentList.add(mSongListFragment);
        mFragmentList.add(mSingFragment);
        mFragmentList.add(mMsgFragment);
        mFragmentList.add(mFindsFragment);

        mFragmentAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return mFragmentList.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

        };

        mViewPager.setAdapter(mFragmentAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //页面滚动事件
            }

            @Override
            public void onPageSelected(int position) {
                //页面选中事件
                setSelectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //页面滚动状态改变事件
            }
        });

        mViewPager.setCurrentItem(1);
        mLastTab = R.id.iv_tab_list;
//        mMyTools.Log("mSongListFragment initKcbData ");
        mSongListFragment.OnShowWindow(mActivity, true);

    }

    private void setInitImgStatus() {
        mTabMyIv.setImageResource(R.drawable.tab_my_normal);
        mTabListIv.setImageResource(R.drawable.tab_list_normal);
        mTabSingIv.setImageResource(R.drawable.tab_sing_normal);
        mTabMsgIv.setImageResource(R.drawable.tab_msg_normal);
        mTabFindIv.setImageResource(R.drawable.tab_find_normal);
    }

    private void setInitTvStatus() {
        mTabMyTv.setTextColor(COLOR_NORMAL);
        mTabListTv.setTextColor(COLOR_NORMAL);
        mTabSingTv.setTextColor(COLOR_NORMAL);
        mTabMsgTv.setTextColor(COLOR_NORMAL);
        mTabFindTv.setTextColor(COLOR_NORMAL);
    }

    private void setSelectTab(int pos) {
        setInitImgStatus();
        setInitTvStatus();
        switch (pos) {
            case 0:
                mTabMyIv.setImageResource(R.drawable.tab_my_select);
                mTabMyTv.setTextColor(COLOR_FOCOUS);
                break;
            case 1:
                mTabListIv.setImageResource(R.drawable.tab_list_select);
                mTabListTv.setTextColor(COLOR_FOCOUS);
                break;
            case 2:
                mTabSingIv.setImageResource(R.drawable.tab_sing_select);
                mTabSingTv.setTextColor(COLOR_FOCOUS);
                break;
            case 3:
                mTabMsgIv.setImageResource(R.drawable.tab_msg_select);
                mTabMsgTv.setTextColor(COLOR_FOCOUS);
                break;
            case 4:
                mTabFindIv.setImageResource(R.drawable.tab_find_select);
                mTabFindTv.setTextColor(COLOR_FOCOUS);
                break;
            default:
                break;
        }
        setViewPagerSelect(pos);
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mUserTabLayout = (LinearLayout) findViewById(R.id.layout_my);

        mTabMyIv = (ImageView) findViewById(R.id.iv_tab_my);
        mTabListIv = (ImageView) findViewById(R.id.iv_tab_list);
        mTabSingIv = (ImageView) findViewById(R.id.iv_tab_sing);
        mTabMsgIv = (ImageView) findViewById(R.id.iv_tab_msg);
        mTabFindIv = (ImageView) findViewById(R.id.iv_tab_user);

        mTabMyTv = (TextView)findViewById(R.id.tv_tab_my);
        mTabListTv = (TextView)findViewById(R.id.tv_tab_list);
        mTabSingTv = (TextView)findViewById(R.id.tv_tab_sing);
        mTabMsgTv = (TextView)findViewById(R.id.tv_tab_msg);
        mTabFindTv = (TextView)findViewById(R.id.tv_tab_find);


        mImageViewList = new ArrayList<ImageView>();
        mImageViewList.add(mTabMyIv);
        mImageViewList.add(mTabListIv);
        mImageViewList.add(mTabSingIv);
        mImageViewList.add(mTabMsgIv);
        mImageViewList.add(mTabFindIv);

        initEventHandler();


    }

    private void initEventHandler() {
        for(int i=0;i<mImageViewList.size();i++){
            mImageViewList.get(i).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (mLastTab){
            case R.id.iv_tab_my: mUserFragment.OnShowWindow(mActivity, false); break;
            case R.id.iv_tab_list: mSongListFragment.OnShowWindow(mActivity, false); break;
            case R.id.iv_tab_sing: mSingFragment.OnShowWindow(mActivity, false); break;
            case R.id.iv_tab_msg: mMsgFragment.OnShowWindow(mActivity, false); break;
            case R.id.iv_tab_user: mFindsFragment.OnShowWindow(mActivity, false); break;
        }
        //mMyTools.Log("onClick "+ v.getId());
        switch (v.getId()){
            case R.id.iv_tab_my: setSelectTab(0); mUserFragment.OnShowWindow(mActivity, true); break;
            case R.id.iv_tab_list: setSelectTab(1); mSongListFragment.OnShowWindow(mActivity, true); break;
            case R.id.iv_tab_sing: setSelectTab(2); mSingFragment.OnShowWindow(mActivity, true); break;
            case R.id.iv_tab_msg: setSelectTab(3); mMsgFragment.OnShowWindow(mActivity, true); break;
            case R.id.iv_tab_user: setSelectTab(4); mFindsFragment.OnShowWindow(mActivity, true); break;
        }
        mLastTab = v.getId();
    }

    private void setViewPagerSelect(int pos){
        mViewPager.setCurrentItem(pos);
    }
}
